let ManagementData = [
    {
        tickBox: "<input type=checkbox>",
        id: 1,
        jobTitle: "UI",
        jobCoding: "De001",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 2,
        jobTitle: "前端开发",
        jobCoding: "De002",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 3,
        jobTitle: "前端开发",
        jobCoding: "De003",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 4,
        jobTitle: "前端开发",
        jobCoding: "De004",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 5,
        jobTitle: "前端开发",
        jobCoding: "De005",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 6,
        jobTitle: "前端开发",
        jobCoding: "De006",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 7,
        jobTitle: "前端开发",
        jobCoding: "De007",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 8,
        jobTitle: "前端开发",
        jobCoding: "De008",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 9,
        jobTitle: "前端开发",
        jobCoding: "De009",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 10,
        jobTitle: "前端开发",
        jobCoding: "De0010",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 11,
        jobTitle: "前端开发",
        jobCoding: "De0011",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 12,
        jobTitle: "前端开发",
        jobCoding: "De0012",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 13,
        jobTitle: "前端开发",
        jobCoding: "De0013",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 14,
        jobTitle: "前端开发",
        jobCoding: "De0014",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 15,
        jobTitle: "前端开发",
        jobCoding: "De0015",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
        operate: "编辑 删除",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 16,
        jobTitle: "前端开发",
        jobCoding: "De0016",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 17,
        jobTitle: "前端开发",
        jobCoding: "De0017",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 18,
        jobTitle: "前端开发",
        jobCoding: "De0018",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 19,
        jobTitle: "前端开发",
        jobCoding: "De0019",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 20,
        jobTitle: "前端开发",
        jobCoding: "De0020",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 21,
        jobTitle: "前端开发",
        jobCoding: "De0021",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 22,
        jobTitle: "前端开发",
        jobCoding: "De0022",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 23,
        jobTitle: "前端开发",
        jobCoding: "De0023",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",

    },
    {
        tickBox: "<input type=checkbox>",
        id: 24,
        jobTitle: "前端开发",
        jobCoding: "De0024",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
    },
    {
        tickBox: "<input type=checkbox>",
        id: 25,
        jobTitle: "前端开发",
        jobCoding: "De0025",
        jobCategory: "研发类",
        departmentAffiliation: "技术部",
        jobDescription: "主要用于前端开发岗",
    },
];
//搜索
let arr = []
$("#search").on("click", function () {
    arr = []
    let value1 = $("#input1").prop("value").trim();
    let value2 = $("#input2").prop("value").trim();
    let value3 = $("#input3").prop("value").trim();
    console.log(value1)
    if (!value1 || !value2 || !value3) {
        renderTable(ManagementData)
        return
    }
    for (let i = 0; i < ManagementData.length; ++i) {
        let a = ManagementData[i].jobTitle;
        let b = ManagementData[i].jobCategory;
        let c = ManagementData[i].departmentAffiliation;
        if (a.includes(value1) && b.includes(value2) && c.includes(value3)) {
            arr.push(ManagementData[i])
        }
    }
    renderTable(arr)
})
const pageSize = 5;
// 渲染表格函数
function renderTable(data, currentPage = 1) {
    renderPaging(data, currentPage);
    const startIndex = pageSize * (currentPage - 1);
    const endIndex = pageSize + startIndex;
    const dataByPaging = data.slice(startIndex, endIndex);
    $("tbody").empty();
    for (let i = 0; i < dataByPaging.length; ++i) {
        let item = dataByPaging[i];
        $("tbody").append(`<tr>
            <td>${item.tickBox}</td>
            <td>${item.id}</td>
            <td>${item.jobTitle}</td>
            <td>${item.jobCoding}</td>
            <td>${item.jobCategory}</td>
            <td>${item.departmentAffiliation}</td>
            <td>${item.jobDescription}</td>
            <td><button data-id=${item.id} class="modify-btn">编辑</button>
            <button data-id=${item.id} class="btn" data-toggle="modal" data-target="#myModal">删除</button></td>
        </tr>`);
    }
}
// 1. 渲染表格
renderTable(ManagementData);
// 2. 渲染分页
function renderPaging(data, currentPage = 1) {
    $(".pagination").empty();
    // $(".pagination").append(`<li><a id="pre" href="#">&laquo;</a></li>`);
    const num = Math.ceil(data.length / pageSize);
    for (let i = 1; i <= num; ++i) {
        $(".pagination").append(`<li><span href="#">${i}</span></li>`);
    }
    $(".pagination > li")
        .eq(currentPage - 1)
        .addClass("currentPageColor");
    // $(".pagination").append(`<li><a id="next" href="#">&raquo;</a></li>`);
}
$(".pagination").on("click", "li", function (event) {
    const currentPage = parseInt($(event.target).text());
    renderTable(ManagementData, currentPage);
});
// 处理上一页
$("#pre").on("click", function () {
    // 获取到当前页
    const currentPage = parseInt($(".pagination > .currentPageColor").text());
    console.log(currentPage)
    // 当前页为1  就不能上一页
    if (currentPage === 1) {
        return;
    }

    renderTable(ManagementData, currentPage - 1);
});
// 处理下一页
$("#next").on("click", function () {
    // 获取到当前页
    const currentPage = parseInt($(".pagination > .currentPageColor").text());

    const max = Math.ceil(ManagementData.length / pageSize);
    // 当前页为 max 就不能下一页
    if (currentPage === max) {
        return;
    }

    renderTable(ManagementData, currentPage + 1);
});
//删除
$("tbody").on("click", ".btn", function (event) {
    const dataId = parseInt($(event.target).attr("data-id"));
    for (let i = 0; i < ManagementData.length; ++i) {
        if (ManagementData[i].id === dataId) {
            ManagementData.splice(i, 1);
        }
    }
});
//确定删除才删除
$("#a").on("click", () => {
    for (let i = 0; i < ManagementData.length; ++i) {
        let item = ManagementData
        let id = ManagementData[i].id
        if (item[i].index === id) {
            ManagementData.splice(i, 1);
        }
    }
    $("#myModal").modal("hide")
    renderTable(ManagementData);
})
//显示弹窗
$(".organization tbody").on("click", ".modify-btn", function (event) {
    const dataId = parseInt($(event.target).attr("data-id"));
    $("#myModal1").modal("show")
    // const currentManagement = ManagementData.find((item) => {
    //     console.log(item)
    //     return item.id = dataId
    // })
    for (let i = 0; i < ManagementData.length; ++i) {
        if (ManagementData[i].id === dataId) {
            $("#ManagementId").val(ManagementData[i].id)
            $("#ManagementJobTitle").val(ManagementData[i].jobTitle)
            $("#ManagementJobCoding").val(ManagementData[i].jobCoding)
            $("#ManagementJobCategory").val(ManagementData[i].jobCategory)
            $("#ManagementDepartmentAffiliation").val(ManagementData[i].departmentAffiliation)
            $("#ManagementJobDescription").val(ManagementData[i].jobDescription)
        }
    }


})
//确认修改
$(".btn-primary").on("click", () => {
    //获取input里的值
    const id = parseInt($("#ManagementId").val());
    const jobTitle = $("#ManagementJobTitle").val();
    const jobCoding = $("#ManagementJobCoding").val();
    const jobCategory = $("#ManagementJobCategory").val();
    const departmentAffiliation = $("#ManagementDepartmentAffiliation").val();
    const jobDescription = $("#ManagementJobDescription").val();
    for (let i = 0; i < ManagementData.length; ++i) {
        const item = ManagementData[i];
        if (item.id === id) {
            // item.id = id;
            item.jobTitle = jobTitle;
            item.jobCoding = jobCoding;
            item.jobCategory = jobCategory;
            item.departmentAffiliation = departmentAffiliation;
            item.jobDescription = jobDescription;
        }
    }
    renderTable(ManagementData);
    $("#myModal1").modal("hide");
});
$("#new").on("click", function () {
    $("#myModal2").modal("show")
})
$("#new2").on("click", function () {
    $("#myModal2").modal("hide")
})